.packageName <- "FLICA"
### FLR core version 0.7-0 ###
### FLICA
### This file contains code just for the core class FLICA

validFLICA.control <- function(object){
	if (any(object@lambda.age < 0))
		return("weights must be > 0")
	if (any(object@lambda.yr < 0))
		return("value of rage must be >= -1")
	if (object@lambda.sr < 0)
		return("SR weight must be >= 0")
	if (!is.na(object@sr.age))
  	if (object@sr.age < 0)
	   	return("sr.age must be >= 0")

	# Everything is fine
	return(TRUE)
}

setClass("FLICA.control",
	representation(
        sep.nyr		   ="integer",   ## Number of years for separable model
		sep.age		   ="integer",   ## Reference age for fitting the separable model
		sep.sel      ="numeric",  ## Are the age-structured indices are correlated across ages
#        sep.2  		 ="integer",   ## Year at which to change selection pattern, if NA only one selection pattern
#		sep.gradual  ="logical",   ## whether change in selection is gradual or step
		sr			 ="logical",   ## Stock and recruitment parameters, i.e. whether to fit a stock-recruit relation
		sr.age       ="integer",   ## age at recruitment
		lambda.age	 ="numeric",   ## Weighting matrices for catch-at-age; for aged surveys; for SSB surveys
		lambda.yr	 ="numeric",   ## Relative weights by year
		lambda.sr	 ="numeric",   ## weight for the SRR term in the objective function
		index.model  ="character", ## 'p' N=aU^b, 'l' N=aU, 'a' N = U
		index.cor    ="numeric"),  ## Are the age-structured indices are correlated across ages
	prototype=prototype(
		sep.nyr		   =as.integer(5),
		sep.age		   =as.integer(4),
		sep.sel	       =as.numeric(1.0),
#        sep.2  		   =as.integer(-1),
#		sep.gradual    =as.logical(TRUE),
		sr			   =as.logical(TRUE),
		sr.age  	   =as.integer(-1),
		lambda.age	   =as.numeric(1.0),
		lambda.yr	   =as.numeric(1.0),
		lambda.sr      =as.numeric(0.1),
		index.model    =as.character("l"),
		index.cor      =as.numeric(0.0)),
	validity=validFLICA.control
)

setValidity("FLICA.control", validFLICA.control)
remove(validFLICA.control)	# We do not need this function any more


## FLICA #######################################################################
validFLICA <- function(object){
	# All FLQuant objects must have same dimensions
	Dim <- dim(object@stock.n)
	if (!all(dim(object@harvest) == Dim))
		return("n and f arrays must have same dimensions")
	# Everything is fine
	return(TRUE)
}

  setClass("FLICA",
    contains="FLAssess",
  	representation(
      call      ="character", 
      catch.res ="FLQuant",    #residuals from seperable analysis log(catch.n/catch.hat)
      param     ="data.frame",      #parameters
      sum.sq   ="data.frame",  #mean square weights
      covar     ="array",      #covar
      q         ="FLQuant",    #catchability by index & age
      q.var     ="FLQuant",    #catchability var by index & age
      f.ref     ="FLQuant",    #the estimate of F for the reference age in the separable model
      f.ref.var ="FLQuant",    #the standard errors about the reference age in F for the reference age
      ssb.var   ="FLQuant",    #the standard errors around the estimate of SSB in the separable periods
      sel       ="FLQuant",    #the selection pattern for the separable period by age
      sel.var   ="FLQuant",    #the standard error of that selection pattern by age
      ica.out   ="character",   #the ica.out documentation file
      resids    ="FLQuants",    #all the residuals from the together in one place
      weighted.resids    ="FLQuants",    #all the weigthed residuals from the together in one place
  		control  ="FLICA.control"),
  	prototype=prototype(
      call      =new("character"), 
      catch.res =new("FLQuant"),
      param     =new("data.frame"),
      sum.sq   =new("data.frame"),
      covar     =new("array"),
      q         =new("FLQuant"),
      q.var     =new("FLQuant"),
      f.ref     =new("FLQuant"),
      f.ref.var =new("FLQuant"),
      ssb.var   =new("FLQuant"),
      sel       =new("FLQuant"),
      sel.var   =new("FLQuant"),
      ica.out   =new("character"),
      resids    =new("FLQuants"),
      weighted.resids=new("FLQuants"),
  		control  =new("FLICA.control")),
  	validity=validFLICA
  )

setValidity("FLICA", validFLICA)
remove(validFLICA)	# We do not need this function any more

### End class ##################################################################


### Methods ####################################################################
